package com.atguigu.cloud.controller;

import com.atguigu.cloud.entites.PayDTO;
import com.atguigu.cloud.entities.Pay;
import com.atguigu.cloud.resp.ResultData;
import com.atguigu.cloud.service.PayService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author zuife
 * @description
 * @since 2024-04-14 15:55:30
 */
@RestController
@Slf4j
@Tag(name = "支付接口",description = "支付接口")
public class PayController {
    @Resource
    private PayService payService;

    @PostMapping("/pay/add")
    @Operation(summary = "添加支付记录")
    public ResultData<String> addPay(@RequestBody Pay pay) {
        log.info(pay.toString());
        int i=payService.add(pay);
        return ResultData.success("成功插入记录，返回值："+i);
    }
    @DeleteMapping("/pay/delete/{id}")
    @Operation(summary = "删除支付记录")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        log.info("删除id为"+id+"的记录");
        return ResultData.success(payService.delete(id));
    }
    @PutMapping("/pay/update")
    @Operation(summary = "更新支付记录")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        Pay pay=new Pay();
        BeanUtils.copyProperties(payDTO,pay);
        int i= payService.update(pay);
        return ResultData.success(i>0?"更新成功":"更新失败");
    }
    @GetMapping("/pay/getById/{id}")
    @Operation(summary = "根据id查询支付记录")
    public ResultData<Pay> getById(@PathVariable("id") Integer id) {
        if(id<0)
            throw new RuntimeException("id不能为负数");
        Pay pay= payService.getById(id);
        return ResultData.success(pay);
    }
    @GetMapping(value = "/pay/getPayError")
    public ResultData<Integer> getPayError(){
        Integer integer = Integer.valueOf(200);

        return ResultData.success(integer);
    }
    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/get/info")
    public String getInfoByConsul(@Value("${atguigu.info}") String atguiguInfo){
        return  "atguiguInfo："+atguiguInfo+"\t"+"port:"+port;
    }
}
